package com.group.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.group.entity.ProjectOutside;
import com.group.entity.Talents;
import com.group.mapper.ProjectOutsideMapper;
import com.group.service.ProjectOutsideService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 朋心合力
 * @since 2021-08-09
 */
@Controller
//@RequestMapping("/project-outside")
public class ProjectOutsideController {

    @Autowired
    ProjectOutsideMapper projectOutsideMapper;

    @Autowired
    ProjectOutsideService projectOutsideService;



    /**
     * @param zone  前端所选条件（最多两个）
     * @param model 视图
     * @param page  当前页
     * @param size  每页大小
     * @return
     */
    @GetMapping("/lookingProjectByTag/{page}/{size}")
    public String lookingProjectsByTag(@RequestParam("zone")String[] zone,Model model,
                                       @PathVariable("page") Integer page,
                                       @PathVariable("size") Integer size) {
        List<ProjectOutside> outsideList = null;
        Page<ProjectOutside> projectOutsidePage = null;
        if (zone.length == 1 && (zone[0].equals("全部技术领域"))) {
            projectOutsidePage = projectOutsideService.getPages(page, size, projectOutsideMapper.selectList(null));
            outsideList = projectOutsidePage.getRecords();
            model.addAttribute("allPages", projectOutsidePage.getPages());
        }
        if (zone.length == 1 && !(zone[0].equals("全部技术领域"))) {
            projectOutsidePage = projectOutsideService.getPages(page, size, projectOutsideService.find(zone[0], null));
            outsideList = projectOutsidePage.getRecords();
            model.addAttribute("allPages", projectOutsidePage.getPages());
//            talentsList1.forEach(System.out::println);
        }
        if (zone.length == 2) {
            projectOutsidePage = projectOutsideService.getPages(page, size, projectOutsideService.find(zone[0], zone[1]));
            outsideList = projectOutsidePage.getRecords();
            model.addAttribute("allPages", projectOutsidePage.getPages());
        }

        model.addAttribute("projectsList", outsideList);
        return "lookingprojects";
//        return outsideList;
    }
    //Postman 测试分页接口成功
//@GetMapping("/projectList/{page}/{size}")
//    @ResponseBody
//    public List<ProjectOutside> projectOutside(Model model,@PathVariable("page") Integer page,@PathVariable("size") Integer size) {
//        IPage<ProjectOutside> projectIPage = new Page<>(page, size);
//        IPage<ProjectOutside> result = projectOutsideService.page(projectIPage, null);
//        List<ProjectOutside> projectOutsideList = result.getRecords();
//        model.addAttribute("projectOutsideList", projectOutsideList);
//        return projectOutsideList;
//    }

}

